package cn.iocoder.an.module.mold.controller.admin.upkeeptask2item;

import cn.iocoder.an.module.mold.controller.admin.upkeepcategory.vo.UpkeepCategoryRespVO;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.upkeeptask2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeeptask2item.UpkeepTask2itemDO;
import cn.iocoder.an.module.mold.service.upkeeptask2item.UpkeepTask2itemService;

@Tag(name = "管理后台 - 模具保养任务以及保养项目对照")
@RestController
@RequestMapping("/mold/upkeep-task2item")
@Validated
public class UpkeepTask2itemController {

    @Resource
    private UpkeepTask2itemService upkeepTask2itemService;

    @PostMapping("/create")
    @Operation(summary = "创建模具保养任务以及保养项目对照")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:create')")
    public CommonResult<Long> createUpkeepTask2item(@Valid @RequestBody UpkeepTask2itemSaveReqVO createReqVO) {
        return success(upkeepTask2itemService.createUpkeepTask2item(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具保养任务以及保养项目对照")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:update')")
    public CommonResult<Boolean> updateUpkeepTask2item(@Valid @RequestBody UpkeepTask2itemSaveReqVO updateReqVO) {
        upkeepTask2itemService.updateUpkeepTask2item(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具保养任务以及保养项目对照")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:delete')")
    public CommonResult<Boolean> deleteUpkeepTask2item(@RequestParam("id") Long id) {
        upkeepTask2itemService.deleteUpkeepTask2item(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具保养任务以及保养项目对照")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:delete')")
    public CommonResult<Boolean> deleteUpkeepTask2itemList(@RequestParam("ids") List<Long> ids) {
        upkeepTask2itemService.deleteUpkeepTask2itemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具保养任务以及保养项目对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:query')")
    public CommonResult<UpkeepTask2itemRespVO> getUpkeepTask2item(@RequestParam("id") Long id) {
        UpkeepTask2itemDO upkeepTask2item = upkeepTask2itemService.getUpkeepTask2item(id);
        return success(BeanUtils.toBean(upkeepTask2item, UpkeepTask2itemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具保养任务以及保养项目对照分页")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:query')")
    public CommonResult<PageResult<UpkeepTask2itemRespVO>> getUpkeepTask2itemPage(@Valid UpkeepTask2itemPageReqVO pageReqVO) {
        PageResult<UpkeepTask2itemDO> pageResult = upkeepTask2itemService.getUpkeepTask2itemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UpkeepTask2itemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具保养任务以及保养项目对照 Excel")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-task2item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUpkeepTask2itemExcel(@Valid UpkeepTask2itemPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UpkeepTask2itemDO> list = upkeepTask2itemService.getUpkeepTask2itemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具保养任务以及保养项目对照.xls", "数据", UpkeepTask2itemRespVO.class,
                BeanUtils.toBean(list, UpkeepTask2itemRespVO.class));
    }


    @GetMapping("/items-of-task")
    @Operation(summary = "获得模具保养任务以及保养项目对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TransMethodResult
    public CommonResult<List<UpkeepTask2itemRespVO>> getUpkeepTask2itemByTaskId(@RequestParam("id") Long taskId) {
        List<UpkeepTask2itemDO> items = upkeepTask2itemService.getUpkeepTask2itemListByTaskId(taskId);
        return success(BeanUtils.toBean(items, UpkeepTask2itemRespVO.class));
    }

}